package com.example.system.controller.exam;

import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.R;
import com.example.common.core.domain.TableDataInfo;
import com.example.system.domain.exam.dto.ExamAddDTO;
import com.example.system.domain.exam.dto.ExamEditDTO;
import com.example.system.domain.exam.dto.ExamQueryDTO;
import com.example.system.domain.exam.dto.ExamQuestionAddDTO;
import com.example.system.domain.exam.vo.ExamDetailVO;
import com.example.system.domain.exam.vo.ExamVO;
import com.example.system.service.exam.IExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private IExamService examService;

    //获取竞赛列表
    // exam/list
    @GetMapping("/list")
    public TableDataInfo list(ExamQueryDTO examQueryDTO) {
        List<ExamVO> list = examService.list(examQueryDTO);
        return getTableDataInfo(list);
    }

    //新增竞赛
    // exam/add
    @PostMapping("/add")
    public R<String> add(@RequestBody ExamAddDTO examAddDTO) {
        String result = examService.add(examAddDTO);
        return R.ok(result);
    }

    //竞赛的题目新增
    @PostMapping("/question/add")
    public R<Void> questionAdd(@RequestBody ExamQuestionAddDTO examQuestionAddDTO) {
        boolean result = examService.questionAdd(examQuestionAddDTO);
        return toR(result);
    }

    //竞赛的题目删除
    @DeleteMapping("/question/delete")
    public R<Void> questionDelete(Long examId, Long questionId) {
        int result = examService.questionDelete(examId, questionId);
        return toR(result);
    }

    /**
     * 获取竞赛详情
     * @param examId
     * @return
     */
    @GetMapping("/detail")
    public R<ExamDetailVO> detail(Long examId) {
        ExamDetailVO examDetailVO = examService.detail(examId);
        return R.ok(examDetailVO);
    }

    @PutMapping("/edit")
    public R<Void> edit(@RequestBody ExamEditDTO examEditDTO) {
        int result = examService.edit(examEditDTO);
        return toR(result);
    }

    @DeleteMapping("/delete")
    public R<Void> delete(Long examId) {
        int result = examService.delete(examId);
        return toR(result);
    }

    @PutMapping("/publish")
    public R<Void> publish(Long examId) {
        return toR(examService.publish(examId));
    }

    @PutMapping("/cancelPublish")
    public R<Void> cancelPublish(Long examId) {
        return toR(examService.cancelPublish(examId));
    }

}
